package com.wf.it.hpwebauth;

import com.wf.it.common.api.UserService;
import com.wf.it.common.entity.User;
import com.wf.it.common.util.JwtUtil;
import com.wf.it.common.vo.Result;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
//@CrossOrigin( exposedHeaders = {"jwt_token"},maxAge = 3600)
public class UserLoginController {
    @Autowired
    private UserService userService;

    @Value("${my.jwt_key}")
    private String jwtkey;
    @Value("${my.jwt_time}")
    private int jwttime;

    @PostMapping("/user/login")
    public Result<User> userLogin(String loginName, String loginPwd, HttpServletResponse resp){
       Result<User> result= userService.userLogin(loginName,loginPwd);
       if(result.getState()!=200)
           return result;

       User user=result.getObj();
       Map<String,Object> map=new HashMap<>();
       map.put("user",user);
       String jwt= JwtUtil.createToken(
               this.jwtkey,jwttime,user.getIdCard()+"",map);
       resp.setHeader("jwt_token",jwt);
       return result;
    }

    @PostMapping("/user/reg")
    public Result userReg(User user){
        return userService.reg(user);
    }


}
